/*
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-02-08 11:32:29
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-09-12 16:49:04
 * @FilePath: /XMate-h5/src/store/modules/common.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from "pinia";
import { getUserInfo, setUserInfo } from "@/utils/auth";
import { getFuncpPointsAPI, getUserInfoAPI } from "@/services/common";
import { searchCustomerType } from "@/services/orderVisit";

// 定义状态枚举
export const FuncStatusEnum = {
  HIDDEN: "HIDDEN", // 隐藏
  GRAYED: "GRAYED", // 置灰
  CUSTOM: "CUSTOM", // 自定义
  SHOW: "SHOW" // 显示
};

export const useCommonStore = defineStore("commonStore", {
  state: () => ({
    funcPoints: [],
    userInfo: {},
    customerTypeArr: []
  }),

  actions: {
    async getFuncpPointsAction() {
      // web端获取不到
      const { accessToken } = getUserInfo() || {};
      if (!accessToken) {
        return;
      }
      const res = await getFuncpPointsAPI(accessToken);
      console.log("getFuncpPointsAPI res", res);
      this.funcPoints = res.datas;
    },

    async getUserInfoAction() {
      const data = await getUserInfoAPI();
      this.userInfo = data;
      if (data) {
        setUserInfo(data);
      }
    },

    getFuncConfig(factorCode, funcCode) {
      const funcPoints = this.funcPoints; // 假设 funcPoints 存在于 state 中
      const factor = funcPoints.find(f => f.factorCode === factorCode);
      if (!factor) {
        // 找不到对应的 默认显示
        return FuncStatusEnum.HIDDEN;
      }
      let isEnable = factor.orgState == 1;
      const funcConfig = factor.functions.find(f => f.funcCode === funcCode);
      if (!funcConfig) {
        return FuncStatusEnum.HIDDEN; // 找不到对应的 funcCode，返回置灰状态
      }
      if (isEnable) {
        // 根据 enableCtrlType 判断启用状态
        if (funcConfig.enableCtrlType === 0) {
          return FuncStatusEnum.SHOW; // 显示
        } else if (funcConfig.enableCtrlType === 1) {
          return FuncStatusEnum.HIDDEN; // 隐藏
        } else if (funcConfig.enableCtrlType === 2) {
          return FuncStatusEnum.CUSTOM; // 自定义
        }
      } else {
        // 根据 forbidCtrlType 判断禁用状态
        if (funcConfig.forbidCtrlType === 0) {
          return FuncStatusEnum.HIDDEN; // 隐藏
        } else if (funcConfig.forbidCtrlType === 1) {
          return FuncStatusEnum.GRAYED; // 置灰
        } else if (funcConfig.forbidCtrlType === 2) {
          return FuncStatusEnum.CUSTOM; // 自定义
        }
      }
      return FuncStatusEnum.HIDDEN; // 默认返回置灰状态
    },

    async searchCustomerTypeAction() {
      this.customerTypeArr = await searchCustomerType();
    },

    isAllowEditCustomer(id) {
      const obj = this.customerTypeArr.find((v) => v.id === id);
      if (null === obj) {
        return false;
      }
      return obj.permissions.indexOf(4) > -1;
    }
  },

  getters: {
    // 智能商品推荐师
    salesProductInApp: state =>
      state.getFuncConfig("sales_product_recomand", "sales_product_in_app") ===
      FuncStatusEnum.SHOW,
    // 智能应用中 是否显示企业侦察机
    salesCompanyInApp: state =>
      state.getFuncConfig("sales_company_info", "sales_company_in_app") ===
      FuncStatusEnum.SHOW,
    // 客户管理企业信息侦查 是否显示企业侦察机
    salesCompanyInCustomer: state =>
      state.getFuncConfig("sales_company_info", "sales_company_in_customer") ===
      FuncStatusEnum.SHOW,
    // 沟通策略企业信息侦查 状态
    salesCompanyInStategy: state =>
      state.getFuncConfig("sales_company_info", "sales_company_in_stategy") ===
      FuncStatusEnum.SHOW,

    // CRM 同步状态筛选
    salesCRMUpdateFilter: state =>
      state.getFuncConfig("sales_crm_update", "sales_crm_update_filter") ===
      FuncStatusEnum.SHOW,

    // CRM 同步状态列
    salesCRMUpdateStatus: state =>
      state.getFuncConfig("sales_crm_update", "sales_crm_update_status") ===
      FuncStatusEnum.SHOW,

    // 是否禁用客户名称较准
    enterNameControl: state => {
      return state.getFuncConfig("salesmate_customer_name", "enter_name_control") ===
        FuncStatusEnum.SHOW
    },

    // 是否存在商机 
    enableBusinessOpportunity: state => {
      return state.getFuncConfig("business_opportunity", "business_opportunity") ===
        FuncStatusEnum.SHOW
    },

    // 是否是toB
    isToB() {
      const { orgBusinessType } = getUserInfo() || {};
      return orgBusinessType === 0;
    },

    // 是否允许创建客户
    enableCreateCustomer: state => {
      return state.enableCreateCustomerArr.length > 0;
    },

    // 权限列表：1-创建客户,2-导入客户,3-编辑客户,4-删除客户
    enableCreateCustomerArr: state => {
      return state.customerTypeArr.filter((v) => v.permissionsStr?.indexOf(1) > -1);
    },

  },

  persist: {
    enabled: true,
    pick: ["funcPoints", "customerTypeArr"],
    key: "common",
    storage: localStorage
  }
});
